

/*结构体*/

#define MAXSIZE 10

typedef struct
{
	ElemType *base;//用于存放内存分配的基地址
	int front;
	int rear;
}cycle_queue;

/*初始化循环队列*/
void queue_init(cycle_queue *q)
{
	q->base = (ElemType *)malloc(MAXSIZE*sizeof(ElemType));
	if(!q->base)
	{
		printf("循环队列空间分配失败!\n");
		exit(0);
		
	}
	q->front = q->rear = 0;
}

/*入队列*/
void insert_queue(cycle_queue *q,ElemType e)
{
	int insert_position = (q->rear + 1) % MAXSIZE;//入队的位置
	
	if( insert_position == q->front)
	{
		printf("队列已满!\n");
		return;
	}
	q->base[q->rear] = e;
	q->rear = insert_position;
}

/*出队列*/
void delete_queue(cycle_queue *q,ElemType *e)
{
	int delete_position = (q->front + 1) % MAXSIZE;//出队的位置
	
	if(delete_position == q->rear)
	{
		printf("队列已空!\n");
		return;
	}
	*e = q->base[q->front];
	q->front = delete_position;
}

/*销毁队列*/
void destory_queue(cycle_queue *q)
{
	int i = 0;
	for(i; i < MAXSIZE; i++)
	{
		free(q->base[i]);
	}
}

